package com.itheima.stock.service.impl;

import cn.hutool.core.date.DateTime;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.stock.constant.InsetConstant;
import com.itheima.stock.constant.PermissionConstant;
import com.itheima.stock.constant.StockConstant;
import com.itheima.stock.mapper.SysPermissionMapper;
import com.itheima.stock.mapper.SysRoleMapper;
import com.itheima.stock.mapper.SysRolePermissionMapper;
import com.itheima.stock.pojo.domain.AddRoleDomain;
import com.itheima.stock.pojo.domain.EditRoleDomain;
import com.itheima.stock.pojo.dto.PermissionsDTO;
import com.itheima.stock.pojo.entity.SysRole;
import com.itheima.stock.pojo.entity.SysRolePermission;
import com.itheima.stock.pojo.vo.req.P;
import com.itheima.stock.pojo.vo.req.PageResult;
import com.itheima.stock.pojo.vo.resp.PermissionsVO;
import com.itheima.stock.pojo.vo.resp.R;
import com.itheima.stock.pojo.vo.resp.ResponseCode;
import com.itheima.stock.process.DataProcess;
import com.itheima.stock.service.RoleService;
import com.itheima.stock.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private DataProcess dataProcess;
    @Autowired
    private SysPermissionMapper sysPermissionMapper;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private SysRolePermissionMapper sysRolePermissionMapper;
    @Override
    public R<PageResult<SysRole>> RolePage(P p) {
        PageHelper.startPage(p.getPageNum(),p.getPageSize());
        List<SysRole> allRoles = sysRoleMapper.getAllRoles();
        PageResult<SysRole> result = new PageResult<>(new PageInfo<>(allRoles));
        return R.ok(result);
    }
    @Override
    public R<List<PermissionsVO>> getPermissions() {
        List<PermissionsDTO> dtos = sysPermissionMapper.getPermissionsDTO();
        List<PermissionsVO> vos = dataProcess.getPermissions(dtos, PermissionConstant.TOP_PERMISSION);
        return R.ok(vos);
    }
    @Override
    public R addRole(AddRoleDomain addRoleDomain) {
        if(addRoleDomain.getName().isEmpty()){
            return R.error(ResponseCode.DATA_ERROR);
        }
        if(sysRoleMapper.getRoleByName(addRoleDomain.getName())){
            return R.error(ResponseCode.ROLE_EXISTS_ERROR);
        }
        if(addRoleDomain.getPermissionsIds().isEmpty()){
            return R.error(ResponseCode.DATA_ERROR);
        }
        SysRole role = new SysRole();
        role.setId(idWorker.nextId());
        role.setName(addRoleDomain.getName());
        role.setDescription(addRoleDomain.getDescription());
        role.setDeleted(InsetConstant.INIT_DELETED);
        role.setStatus(InsetConstant.INIT_STATUS);
        role.setCreateTime(DateTime.now());
        role.setUpdateTime(DateTime.now());
        sysRoleMapper.insert(role);
        SysRolePermission rolePermission = new SysRolePermission();
        for (Long permissionsId : addRoleDomain.getPermissionsIds()) {
            rolePermission.setId(idWorker.nextId());
            rolePermission.setRoleId(role.getId());
            rolePermission.setPermissionId(permissionsId);
            rolePermission.setCreateTime(DateTime.now());
            sysRolePermissionMapper.insertSelective(rolePermission);
        }
        return R.ok(ResponseCode.SUCCESS);
    }

    @Override
    public R<List<String>> getRolePermission(String roleId) {
        List<String> pids = sysRolePermissionMapper.getPermissionByRole(roleId);
        return R.ok(pids);
    }
    @Override
    public R editRole(EditRoleDomain editRoleDomain) {

        System.out.println(editRoleDomain);
        String rid = editRoleDomain.getId();
        sysRolePermissionMapper.delPermissionByRid(rid);
        SysRolePermission rolePermission = new SysRolePermission();
        for (Long permissionsId : editRoleDomain.getPermissionsIds()) {
            rolePermission.setId(idWorker.nextId());
            rolePermission.setRoleId(Long.valueOf(rid));
            rolePermission.setPermissionId(permissionsId);
            rolePermission.setCreateTime(DateTime.now());
            sysRolePermissionMapper.insertSelective(rolePermission);
        }
        return R.ok(ResponseCode.SUCCESS);
    }
    @Override
    public R delRole(String roleId) {
        if(!sysRolePermissionMapper.getPermissionByRole(roleId).isEmpty())
            return R.error(ResponseCode.ROLE_PERMISSION_RELATION);
        sysRoleMapper.deleteByPrimaryKey(Long.valueOf(roleId));
        return R.ok(ResponseCode.SUCCESS);
    }
    @Override
    public R editRoleStatus(String roleId, Integer status) {
        sysRoleMapper.editRoleStatus(roleId,status);
        return  R.ok(ResponseCode.SUCCESS);
    }
}
